package com.duolaibei.business.view.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.duolaibei.business.R;
import com.duolaibei.business.api.ApiName;
import com.duolaibei.business.base.BaseActivity;
import com.duolaibei.business.config.Preferences;
import com.duolaibei.business.http.HttpUtils;
import com.duolaibei.business.model.ResponseResult;
import com.duolaibei.business.utils.CommonUtil;
import com.duolaibei.business.utils.GsonUtil;
import com.duolaibei.business.utils.PreferenceUtil;
import com.duolaibei.business.utils.inject.Inject;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by xlm on 2017/11/14 0014
 */

public class AccountInfoActivity extends BaseActivity {

    @Inject(R.id.back)
    private RelativeLayout mLayoutBack;
    @Inject(R.id.title)
    private TextView mTextViewTitle;

    @Inject(R.id.user_name)
    private TextView mViewName;
    @Inject(R.id.user_cardnum)
    private TextView mViewCardnum;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_account_info);
    }

    @Override
    protected void initViews() {
        mTextViewTitle.setText("收款账户信息");
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        getAccountInfo();
    }

    @Override
    protected void onSetListener() {
        mLayoutBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     * 获取收款账号信息
     */
    private void getAccountInfo() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", ApiName.MERCHANT_INFO);
        param.add("uid", String.valueOf(PreferenceUtil.getParam(Preferences.UID, "")));
        param.add("login_type", String.valueOf(PreferenceUtil.getParam(Preferences.LOGINTYPE, "")));
        param.add("login_id", String.valueOf(PreferenceUtil.getParam(Preferences.LOGINID, "")));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().url(ApiName.API).params(param)
                .executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                if (CommonUtil.isForeground(getThis())) {
                    showToast(getString(R.string.net_state));
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                if (HttpUtils.isJson(string)) {
                    ResponseResult result = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (result.getStatus() == 1) {
//                        {
//                            "form": [],
//                            "info": "获取收款账号信息成功",
//                                "status": 1,
//                                "data": {
//                            "bank_card": "6217********4674",
//                                    "bank_account_name": "谢黎明"
//                        }
//                        }
                        try {
                            JSONObject jsonObject = new JSONObject(string);
                            JSONObject jsonObject1 = jsonObject.optJSONObject("data");
                            final String name = jsonObject1.optString("bank_account_name");
                            final String card = jsonObject1.optString("bank_card");
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    mViewName.setText(name);
                                    mViewCardnum.setText(card);
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        showToast(result.getInfo());
                    }
                } else {
                    showToast(string);
                }
            }
        });
    }
}
